package com.swas.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.PatrolPoint;
import com.swas.entity.PatrolPointExample;
import com.swas.dao.PatrolPointMapper;
import com.swas.service.PatrolPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class PatrolPointServiceImpl implements PatrolPointService {

    @Autowired
    private PatrolPointMapper patrolPointMapper;


    @Override
    public PageInfo<PatrolPoint> list(Integer pageNum, Integer pageSize, String pointNo, String pointName, String address) {
        PageHelper.startPage(pageNum, pageSize);
        PatrolPointExample example = new PatrolPointExample();
        PatrolPointExample.Criteria criteria = example.createCriteria();
        if (pointNo != null && !pointNo.equals("")) {
            criteria.andPointnoEqualTo(pointNo);
        }
        if (pointName != null && !pointName.equals("")) {
            criteria.andPointnameLike("%" + pointName + "%");
        }
        if (address != null && !address.equals("")) {
            criteria.andAddressLike("%"+address+"%");
        }
        List<PatrolPoint> list = patrolPointMapper.selectByExample(example);
        PageInfo<PatrolPoint> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void add(PatrolPoint patrolPoint) {
        patrolPointMapper.insertSelective(patrolPoint);
    }

    @Override
    public void update(PatrolPoint patrolPoint) {
        patrolPointMapper.updateByPrimaryKeySelective(patrolPoint);
    }

    @Override
    public void delete(Integer id) {
        patrolPointMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PatrolPoint> getPatrolPointByTaskId(Integer taskId) {
        return patrolPointMapper.selectByTaskId(taskId);
    }

    @Override
    public List<PatrolPoint> getPatrolPointByPlanId(Integer planId) {
        return patrolPointMapper.selectByPlanId(planId);
    }

    @Override
    public List<PatrolPoint> all() {
        return patrolPointMapper.selectByExample(null);
    }

    @Override
    public PatrolPoint getPatrolPointByPointNo(String pointNo) {
        PatrolPointExample example = new PatrolPointExample();
        PatrolPointExample.Criteria criteria = example.createCriteria();
        criteria.andPointnoEqualTo(pointNo);
        List<PatrolPoint> list = patrolPointMapper.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

}
